package team.yidong.fenda.controller;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import team.yidong.fenda.entity.MasterScore;
import team.yidong.fenda.service.HospitalService;
import team.yidong.fenda.service.MasterScoreService;
import team.yidong.fenda.service.MasterService;

import javax.servlet.http.HttpSession;

/**
 * Created by xuanlong on 2017/10/29.
 */
@Controller
public class MasterScoreController {
    @Autowired
    MasterScoreService masterScoreService;
    @Autowired
    HospitalService hospitalService;
    @Autowired
    MasterService masterService;

    @RequestMapping("/masterScore")
    public String masterScore(Model model, HttpSession session) {
        Integer hospitalId = (Integer) session.getAttribute("loginHospitalId");
        String sysName = (String) session.getAttribute("loginSysUserName");
        model.addAttribute("sysName", sysName);
        if (hospitalId != 1) {
            JsonConfig jsonConfig = new JsonConfig();
            jsonConfig.setExcludes(new String[]{"hospitalPermissionSet", "master", "hospitalImgSet", "hospitalFieldList", "userSet", "masterList", "answer", "praiseUserSet", "answerList", "question", "masterScoreAnswer"});
            model.addAttribute("hospitals", JSONArray.fromObject(hospitalService.findHospitalById(hospitalId), jsonConfig));
            model.addAttribute("masters", JSONArray.fromObject(masterService.findByHospital(hospitalId), jsonConfig));
        } else {
            JSONArray jsonArray = hospitalService.findAll();
            jsonArray.remove(0);
            model.addAttribute("hospitals", jsonArray);
            JsonConfig jsonConfig = new JsonConfig();
            jsonConfig.setExcludes(new String[]{"hospitalPermissionSet", "master", "hospitalImgSet", "hospitalFieldList", "userSet", "masterList", "answer", "praiseUserSet", "answerList", "question", "masterScoreAnswer"});
            model.addAttribute("masters", JSONArray.fromObject(masterService.findAll(), jsonConfig));
        }

        return "masterScore";
    }

    @RequestMapping("/findMasterScore")
    @ResponseBody
    public JSONObject findMasterScore(Model model, HttpSession session, Integer offset, Integer limit, String hospitalName, String masterName, String startDate, String endDate, String status, String tixianDate, String tixianDate1) {
        Integer hospitalId = (Integer) session.getAttribute("loginHospitalId");
        JSONObject j = masterScoreService.findMasterScore(hospitalId, offset, limit, hospitalName, masterName, startDate, endDate, status, tixianDate, tixianDate1);
        return j;
    }


    @RequestMapping("/deleteMasterScore")
    @ResponseBody
    public String deleteMasterScore(@RequestParam String jsonData) {
        JSONArray jsonArray = JSONArray.fromObject(jsonData);
        Iterable<MasterScore> masterScoreIterable = JSONArray.toCollection(jsonArray, MasterScore.class);
        masterScoreService.deleteMasterScore(masterScoreIterable);
        System.out.println("shanchuchengg");
        return "删除成功";

    }

    @RequestMapping("/updateMasterScore")
    @ResponseBody
    public String updateMasterScore(@RequestParam String jsonData) {
        JSONObject jsonObject = JSONObject.fromObject(jsonData);
        MasterScore masterScore = (MasterScore) JSONObject.toBean(jsonObject, MasterScore.class);
        masterScoreService.updateMasterScore(masterScore);
        return "修改成功";
    }
}
